package com.hj.chartdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hj.chartdemo.R;
import com.hj.chartdemo.bean.chart.ChartItemData;
import java.util.ArrayList;
import androidx.recyclerview.widget.RecyclerView;

public class ChartPieAdapter extends RecyclerView.Adapter<ChartPieAdapter.Vh> implements View.OnClickListener {

    private Context context;
    public ArrayList<ChartItemData> stringList;
    public Integer imgType = 1;

    private OnItemClickListener mItemClickListener;

    public ChartPieAdapter(Context context, ArrayList<ChartItemData> stringList) {
        this.context = context;
        this.stringList = stringList;
    }

    public ChartPieAdapter(Context context) {
        this.context = context;
    }

    public void setData(ArrayList<ChartItemData> list) {
        this.stringList = list;
        notifyDataSetChanged();
    }

    public void refresh() {
        notifyDataSetChanged();
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    @Override
    public ChartPieAdapter.Vh onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.chart_pie_item, null);
        Vh holder = new Vh(view);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(ChartPieAdapter.Vh holder, final int position) {
        ChartItemData data = stringList.get(position);
        holder.tv.setText(data.getLabel());
        switch(position){
            case 0 :
                holder.iv.setBackgroundResource(R.drawable.circular_chart_1);
                break;
            case 1 :
                holder.iv.setBackgroundResource(R.drawable.circular_chart_2);
                break;
            case 2 :
                holder.iv.setBackgroundResource(R.drawable.circular_chart_3);
                break;
            case 3 :
                holder.iv.setBackgroundResource(R.drawable.circular_chart_4);
                break;
            case 4 :
                holder.iv.setBackgroundResource(R.drawable.circular_chart_5);
                break;
            default :
                holder.iv.setBackgroundResource(R.drawable.circular_chart_6);
                break;
        }
        holder.itemView.setTag(data);
    }

    @Override
    public int getItemCount() {
        return stringList == null ? 0 : stringList.size();
    }

    @Override
    public void onClick(View v) {
        if (mItemClickListener == null) return;
        ChartItemData appParam = (ChartItemData) v.getTag();
        mItemClickListener.onItemClick(appParam);
    }

    public interface OnItemClickListener {
        void onItemClick(ChartItemData data);
    }

    class Vh extends RecyclerView.ViewHolder {
        public Vh(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.pie_title);
            iv = itemView.findViewById(R.id.pie_color);
        }
        private TextView tv;
        private View iv;
    }
}
